# -*- coding: utf-8 -*-
import tkinter as tk
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from openpyxl import workbook
from lxml import etree
import time


class LingYing_Spider(object):
    def __init__(self):
        # GUI实例化，创建根窗口
        self.window = tk.Tk()
        # 根窗口添加标题
        self.window.title('领英自动化')
        # 根窗口固定大小
        self.window.geometry('600x400')
        # 嵌套一个提示窗口
        self.text_hint_1 = tk.Label(self.window, text='请输入查询的公司', bg='green', fg='white', font=12)
        self.text_hint_1.pack()
        # 嵌套一个输入窗口
        self.text_input_1 = tk.Entry(self.window, font=12, show=None)
        self.text_input_1.pack()
        # 嵌套一个提示窗口
        self.text_hint_2 = tk.Label(self.window, text='请输入你的账号', bg='green', fg='white', font=12)
        self.text_hint_2.pack()
        # 嵌套一个输入窗口
        self.text_input_2 = tk.Entry(self.window, font=12, show=None)
        self.text_input_2.pack()
        # 嵌套一个提示窗口
        self.text_hint_3 = tk.Label(self.window, text='请输入你的密码', bg='green', fg='white', font=12)
        self.text_hint_3.pack()
        # 嵌套一个输入窗口
        self.text_input_3 = tk.Entry(self.window, font=12, show=None)
        self.text_input_3.pack()
        # 嵌套一个运行函数的按钮
        self.button = tk.Button(self.window, text='点击运行', font=12, bg='red', fg='white', command=self.login_html)
        self.button.pack()
        # 运行提示
        # self.text_hint_2 = tk.Text(self.window, width=85, height=20, font=12)
        # self.text_hint_2.pack()
        # 创建一个数据表
        self.wb = workbook.Workbook()
        # 数据表的一个激活
        self.ws = self.wb.active
        # 添加一下数据表的表头
        self.ws.append(['发布内容', '内容图片url', '点赞数', '转发数', '评论数'])
        # 根窗口循环
        self.window.mainloop()


    def login_html(self):
        # 创建一个变量接受输入窗口的值
        str_company = self.text_input_1.get()
        str_account = self.text_input_2.get()
        str_password = self.text_input_3.get()
        # 隐藏自动化头
        options = webdriver.ChromeOptions()
        options.add_experimental_option('excludeSwitches', ['enable-automation'])
        # 实例化自动化对象
        #
        driver = webdriver.Chrome(options=options, executable_path='chromedriver.exe')
        driver.get('https://www.linkedin.com/authwall?trk=ripf&trkInfo=AQED7qDsuPIMsgAAAX-5l4-AxlGf3Dw5x9hSclATswrbbkcwivzM6S2zWl1_IPtflN3Kiig76mHqmspc1Yv4y56TvYxw37LLA3VzFKg3hGMRIUn4oYXeAxBzy-JRuLwt3C40DXk=&originalReferer=&sessionRedirect=https%3A%2F%2Fwww.linkedin.com%2Fcompany%2Fwhite-lodging%2Fposts%2F%3FfeedView%3Dall')
        wait = WebDriverWait(driver, 100)
        # 点击登录按钮
        driver.find_element(By.XPATH, '//*[@id="main-content"]/div/form/p/button').click()
        # 输入账号
        session_key = driver.find_element(By.XPATH, '//*[@id="session_key"]')
        session_key.send_keys(f'{str_account}')
        # 输入密码
        session_password = driver.find_element(By.XPATH, '//*[@id="session_password"]')
        session_password.send_keys(f'{str_password}')
        # 点击登录
        driver.find_element(By.XPATH, '//*[@id="main-content"]/div/div/div/form/button').click()
        time.sleep(40)
        # 点击搜索
        driver.find_element(By.XPATH, '//*[@id="global-nav-typeahead"]/input').click()
        # White Lodging
        driver.find_element(By.XPATH, '//*[@id="global-nav-typeahead"]/input').send_keys(f'{str_company}')
        driver.find_element(By.XPATH, '//*[@id="global-nav-typeahead"]/input').send_keys(Keys.ENTER)
        print('点击搜索了进入搜索界面')
        time.sleep(8)
        # 点击进入公司主页
        print('点击进入公司主页')
        driver.find_element(By.XPATH, '//*[@id="main"]/div/div/div[1]/div/a/div/div[2]/div[2]/a/div/span').click()
        print('开始准备点击posts')
        time.sleep(5)
        # 点击posts
        for tag in driver.find_elements(By.XPATH, '//div[@class="mb4"]//ul[contains(@class, "org-page-navigation__items")]/li/a'):
            # print(tag.text)
            if tag.text.strip() == 'Posts':
                tag.click()
                break
            else:
                continue
        time.sleep(5)
        print('点击完成')
        # 为判断页面下拉距离设置一个初始值
        check_height = 0
        # 进入循环
        while True:
            # 页面无限下拉
            js = 'window.scrollTo(0,document.body.scrollHeight)'
            # 运行js代码
            driver.execute_script(js)
            # 沉睡，让滚动条有所反应
            time.sleep(3)
            # 测试出滚动条距离页面顶端的距离
            finally_height = driver.execute_script("return document.documentElement.scrollHeight || document.body.scrollHeight;")
            # 开始进行判断
            if check_height == finally_height:
                # 如果页面下拉距离与初始值相等，即退出下拉循环
                break
            else:
                # 否则，将下拉值给初始值，为下一次下拉循环做准备
                check_height = finally_height
        self.parse_html(driver)

    def parse_html(self, driver):
        time.sleep(10)
        for small_div in driver.find_elements(By.XPATH, '//div[@class="scaffold-finite-scroll__content"]/div'):
            # WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//div[@class="social-details-social-activity update-v2-social-activity"]//ul/li[contains(@class, "social-details-social-counts__item social-details-social-counts__comments")]/button/span')))
            print('开始准备点击')
            small_div.find_element(By.XPATH, '//*[@id="main"]/div[2]/div/div[2]/div[3]/div/div[1]/div/div/div/div/div[@class="social-details-social-activity update-v2-social-activity"]/div[contains(@class, "feed-shared-social-actions")]/span[@class="comment feed-shared-social-action-bar__action-button"]').click()

            print('点击完成')
            # text_1 = small_div.find_element(By.XPATH, '//div[@class="feed-shared-update-v2__description-wrapper"]//span[@dir="ltr"]')
            # print(text_1.text)
            # print(small_div)


            # 转换源代码格式
            html_data = small_div.get_attribute("outerHTML")
            xml_data = etree.HTML(html_data)
            list_total = []

            # 提取发布内容
            text_data = ' '.join(xml_data.xpath('//div[@class="occludable-update ember-view"]/div/div//div[@class="feed-shared-update-v2__description-wrapper"]//span[@dir="ltr"]/text()'))
            text_url_data = ','.join(xml_data.xpath('//div[@class="occludable-update ember-view"]/div/div//div[@class="feed-shared-update-v2__description-wrapper"]//span[@dir="ltr"]/a/@href'))
            text_total_data = text_data.strip() + text_url_data.strip()
            print(text_total_data)
            list_total.append(text_total_data)

            # # 获取发布内容的url
            # if 'feed-shared-mini-update-v2 feed-shared-update-v2__update-content-wrapper artdeco-card' in html_data:
            #     # 如果是转发的链接
            #     for url_small_data in xml_data.xpath('//div[@class="occludable-update ember-view"]/div/div//div[@class="feed-shared-mini-update-v2 feed-shared-update-v2__update-content-wrapper artdeco-card"]//a[@class="tap-target feed-shared-mini-update-v2__link-to-details-page text-body-medium ember-view"]/@href'):
            #         url_data_1 = 'https://www.linkedin.com' + url_small_data
            #         print(url_data_1)
            #         list_total.append(url_data_1)
            # elif 'media-player video-s-loader__video-container  ember-view' in html_data:
            #     # 如果是视频
            #     for url_video_data in xml_data.xpath('//div[@class="occludable-update ember-view"]/div/div//div[@class="media-player video-s-loader__video-container  ember-view"]/div/video/@src'):
            #         print(url_video_data)
            #         list_total.append(url_video_data)
            # elif 'feed-shared-article feed-shared-update-v2__content' in html_data:
            #     # 如果是小链接
            #     for small_url_data in xml_data.xpath('//div[@class="occludable-update ember-view"]/div/div//article[@class="feed-shared-article feed-shared-update-v2__content"]//a/@href'):
            #         print(small_url_data)
            #         list_total.append(small_url_data)
            # elif 'feed-shared-image__container' in html_data:
            #     # 如果是图片
            #     img_list = []
            #     for url_data_2 in xml_data.xpath('//div[@class="occludable-update ember-view"]/div/div//div[@class="feed-shared-image__container"]//img/@src'):
            #         print(url_data_2)
            #         img_list.append(url_data_2)
            #     list_total.append(','.join(img_list))
            #
            # # 获取发布时的点赞
            # like_data = xml_data.xpath('//div[@class="occludable-update ember-view"]/div/div//div[@class="social-details-social-activity update-v2-social-activity"]/ul/li[1]//span[@class="social-details-social-counts__reactions-count" or @class="social-details-social-counts__social-proof-fallback-number"]/text()')
            # if len(like_data) == 0:
            #     like_data = 0
            #     print(like_data)
            #     list_total.append(list_total)
            # else:
            #     print(like_data[0])
            #     list_total.append(like_data[0])
            #
            # # 获取发布时的转发量
            # share_data = xml_data.xpath('//div[@class="occludable-update ember-view"]/div/div//div[@class="social-details-social-activity update-v2-social-activity"]/ul/li/div[contains(@class, "t-black--light")]/span/text()')
            # if len(share_data) == 0:
            #     share_data = '0 share'
            #     print(share_data)
            #     list_total.append(share_data)
            # else:
            #     print(share_data[0])
            #     list_total.append(share_data[0])
            # # 获取发布的时的评论数
            # comment_data = xml_data.xpath('//div[@class="occludable-update ember-view"]/div/div//div[@class="social-details-social-activity update-v2-social-activity"]/ul/li[contains(@class, "social-details-social-counts__item social-details-social-counts__comments")]/button/span/text()')
            # if len(comment_data) == 0:
            #     comment_data = '0 comment'
            #     print(comment_data)
            #     list_total.append(comment_data)
            # else:
            #     print(comment_data[0].strip())
            #     list_total.append(comment_data[0].strip())
            #
            # # 保存数据到表格
            # print(list_total)
            # self.save_excel(list_total)
            # time.sleep(20)


    def save_excel(self,list_data):
        try:
            self.ws.append(list_data)
            self.wb.save('数据.xlsx')
            print('该数据已加载')
        except:
            print('数据没加载出来')

        # self.excel_data.close()


if __name__ == '__main__':
    a = LingYing_Spider()
    a.login_html()
